<template>
	<el-main class="bg">
		<el-form ref="form" :model="form" status-icon label-width="120px" v-if="is_view()">
			<el-col
				v-if="user_group === '管理员' || $check_field('get', 'event_name') || $check_field('add', 'event_name') || $check_field('set', 'event_name')"
				:xs="24" :sm="12" :lg="8">
				<el-form-item label="赛事名称" prop="event_name">
					<el-input id="event_name" v-model="form['event_name']" placeholder="请输入赛事名称"
						v-if="user_group === '管理员' || (form['individual_registration_id'] && $check_field('set', 'event_name')) || (!form['individual_registration_id'] && $check_field('add', 'event_name'))"
						:disabled="disabledObj['event_name_isDisabled']"></el-input>
					<div v-else-if="$check_field('get', 'event_name')">{{ form['event_name'] }}</div>
				</el-form-item>
			</el-col>
			<el-col
				v-if="user_group === '管理员' || $check_field('get', 'event_type') || $check_field('add', 'event_type') || $check_field('set', 'event_type')"
				:xs="24" :sm="12" :lg="8">
				<el-form-item label="赛事类型" prop="event_type">
					<el-input id="event_type" v-model="form['event_type']" placeholder="请输入赛事类型"
						v-if="user_group === '管理员' || (form['individual_registration_id'] && $check_field('set', 'event_type')) || (!form['individual_registration_id'] && $check_field('add', 'event_type'))"
						:disabled="disabledObj['event_type_isDisabled']"></el-input>
					<div v-else-if="$check_field('get', 'event_type')">{{ form['event_type'] }}</div>
				</el-form-item>
			</el-col>
			<el-col
				v-if="user_group === '管理员' || $check_field('get', 'user_name') || $check_field('add', 'user_name') || $check_field('set', 'user_name')"
				:xs="24" :sm="12" :lg="8">
				<el-form-item label="用户姓名" prop="user_name">
					<div v-if="user_group !== '管理员'">
						{{ get_user_session_user_name(form['user_name']) }}
						<!--<el-input id="business_name" v-model="form['user_name']" placeholder="请输入用户姓名"-->
						<!--v-if="user_group === '管理员' || (form['individual_registration_id'] && $check_field('set','user_name')) || (!form['individual_registration_id'] && $check_field('add','user_name'))" :disabled="disabledObj['user_name_isDisabled']"></el-input>-->
						<!--<div v-else-if="$check_field('get','user_name')">{{form['user_name']}}</div>-->
						<el-select
							v-if="user_group === '管理员' || (form['individual_registration_id'] && $check_field('set', 'user_name')) || (!form['individual_registration_id'] && $check_field('add', 'user_name'))"
							id="user_name" v-model="form['user_name']" :disabled="disabledObj['user_name_isDisabled']">
							<el-option v-for="o in list_user_user_name" :key="o['username']"
								:label="o['nickname'] + '-' + o['username']" :value="o['user_id']">
							</el-option>
						</el-select>
						<el-select v-else-if="$check_field('get', 'user_name')" id="user_name" v-model="form['user_name']"
							:disabled="true">
							<el-option v-for="o in list_user_user_name" :key="o['username']"
								:label="o['nickname'] + '-' + o['username']" :value="o['user_id']">
							</el-option>
						</el-select>
					</div>
					<el-select v-else id="user_name" v-model="form['user_name']" :disabled="disabledObj['user_name_isDisabled']">
						<el-option v-for="o in list_user_user_name" :key="o['username']"
							:label="o['nickname'] + '-' + o['username']" :value="o['user_id']">
						</el-option>
					</el-select>
				</el-form-item>
			</el-col>
			<el-col
				v-if="user_group === '管理员' || $check_field('get', 'college_name') || $check_field('add', 'college_name') || $check_field('set', 'college_name')"
				:xs="24" :sm="12" :lg="8">
				<el-form-item label="学院名称" prop="college_name">
					<el-select id="college_name" v-model="form['college_name']" placeholder="请选择学院名称"
						v-if="user_group === '管理员' || (form['individual_registration_id'] && $check_field('set', 'college_name')) || (!form['individual_registration_id'] && $check_field('add', 'college_name'))"
						:disabled="disabledObj['college_name_isDisabled']">
						<el-option v-for="college in college_list" :key="college" :label="college" :value="college"></el-option>
					</el-select>
					<div v-else-if="$check_field('get', 'college_name')">{{ form['college_name'] }}</div>
				</el-form-item>
			</el-col>
			<el-col
				v-if="user_group === '管理员' || $check_field('get', 'class_name') || $check_field('add', 'class_name') || $check_field('set', 'class_name')"
				:xs="24" :sm="12" :lg="8">
				<el-form-item label="班级名称" prop="class_name">
					<el-input id="class_name" v-model="form['class_name']" placeholder="请输入班级名称"
						v-if="user_group === '管理员' || (form['individual_registration_id'] && $check_field('set', 'class_name')) || (!form['individual_registration_id'] && $check_field('add', 'class_name'))"
						:disabled="disabledObj['class_name_isDisabled']"></el-input>
					<div v-else-if="$check_field('get', 'class_name')">{{ form['class_name'] }}</div>
				</el-form-item>
			</el-col>
			<el-col
				v-if="user_group === '管理员' || $check_field('get', 'grade_name') || $check_field('add', 'grade_name') || $check_field('set', 'grade_name')"
				:xs="24" :sm="12" :lg="8">
				<el-form-item label="年级名称" prop="grade_name">
					<el-input id="grade_name" v-model="form['grade_name']" placeholder="请输入年级名称"
						v-if="user_group === '管理员' || (form['individual_registration_id'] && $check_field('set', 'grade_name')) || (!form['individual_registration_id'] && $check_field('add', 'grade_name'))"
						:disabled="disabledObj['grade_name_isDisabled']"></el-input>
					<div v-else-if="$check_field('get', 'grade_name')">{{ form['grade_name'] }}</div>
				</el-form-item>
			</el-col>
			<el-col
				v-if="user_group === '管理员' || $check_field('get', 'professional_name') || $check_field('add', 'professional_name') || $check_field('set', 'professional_name')"
				:xs="24" :sm="12" :lg="8">
				<el-form-item label="专业名称" prop="professional_name">
					<el-input id="professional_name" v-model="form['professional_name']" placeholder="请输入专业名称"
						v-if="user_group === '管理员' || (form['individual_registration_id'] && $check_field('set', 'professional_name')) || (!form['individual_registration_id'] && $check_field('add', 'professional_name'))"
						:disabled="disabledObj['professional_name_isDisabled']"></el-input>
					<div v-else-if="$check_field('get', 'professional_name')">{{ form['professional_name'] }}</div>
				</el-form-item>
			</el-col>
			<el-col
				v-if="user_group === '管理员' || $check_field('get', 'contact_number') || $check_field('add', 'contact_number') || $check_field('set', 'contact_number')"
				:xs="24" :sm="12" :lg="8">
				<el-form-item label="联系电话" prop="contact_number">
					<el-input id="contact_number" v-model="form['contact_number']" placeholder="请输入联系电话"
						v-if="user_group === '管理员' || (form['individual_registration_id'] && $check_field('set', 'contact_number')) || (!form['individual_registration_id'] && $check_field('add', 'contact_number'))"
						:disabled="disabledObj['contact_number_isDisabled']"></el-input>
					<div v-else-if="$check_field('get', 'contact_number')">{{ form['contact_number'] }}</div>
				</el-form-item>
			</el-col>
			<el-col
				v-if="user_group === '管理员' || $check_field('get', 'user_mailbox') || $check_field('add', 'user_mailbox') || $check_field('set', 'user_mailbox')"
				:xs="24" :sm="12" :lg="8">
				<el-form-item label="用户邮箱" prop="user_mailbox">
					<el-input id="user_mailbox" v-model="form['user_mailbox']" placeholder="请输入用户邮箱"
						v-if="user_group === '管理员' || (form['individual_registration_id'] && $check_field('set', 'user_mailbox')) || (!form['individual_registration_id'] && $check_field('add', 'user_mailbox'))"
						:disabled="disabledObj['user_mailbox_isDisabled']"></el-input>
					<div v-else-if="$check_field('get', 'user_mailbox')">{{ form['user_mailbox'] }}</div>
				</el-form-item>
			</el-col>
			<el-col :xs="24" :sm="12" :lg="8">
				<el-form-item>
					<el-button type="primary" @click="submit()">提交</el-button>
					<el-button @click="cancel()">取消</el-button>
				</el-form-item>
			</el-col>

		</el-form>
	</el-main>
</template>

<script>
import mixin from "@/mixins/page.js";

export default {
	mixins: [mixin],
	data() {
		return {
			field: "individual_registration_id",
			url_add: "~/api/individual_registration/add?",
			url_set: "~/api/individual_registration/set?",
			url_get_obj: "~/api/individual_registration/get_obj?",
			url_upload: "~/api/individual_registration/upload?",

			query: {
				"individual_registration_id": 0,
			},
			college_list: ['计算机学院', '材料与食品学院', '经贸学院', '管理学院', '外国语学院', '电子信息学院', '机电工程学院', '艺术设计学院', '人文社会科学学院', '继续教育学院'],
			form: {
				"event_name": '', // 赛事名称
				"event_type": '', // 赛事类型
				"user_name": 0, // 用户姓名
				"college_name": '', // 学院名称
				"class_name": '', // 班级名称
				"grade_name": '', // 年级名称
				"professional_name": '', // 专业名称
				"contact_number": '', // 联系电话
				"user_mailbox": '', // 用户邮箱
				"individual_registration_id": 0, // ID

			},
			disabledObj: {
				"event_name_isDisabled": false,
				"event_type_isDisabled": false,
				"user_name_isDisabled": false,
				"college_name_isDisabled": false,
				"class_name_isDisabled": false,
				"grade_name_isDisabled": false,
				"professional_name_isDisabled": false,
				"contact_number_isDisabled": false,
				"user_mailbox_isDisabled": false,
			},
			// 用户列表
			list_user_user_name: [],
			// 用户组
			group_user_user_name: "",

		}
	},
	methods: {
		/**
		 * 获取普通用户用户列表
		 */
		async get_list_user_user_name() {
			var json = await this.$get("~/api/user/get_list?user_group=普通用户");
			if (json.result && json.result.list) {
				this.list_user_user_name = json.result.list;
			}
			else if (json.error) {
				console.error(json.error);
			}
		},
		/**
		 * 获取普通用户用户组
		 */
		async get_group_user_user_name() {
			this.form["user_name"] = this.user.user_id;
			var json = await this.$get("~/api/user_group/get_obj?name=普通用户");
			if (json.result && json.result.obj) {
				this.group_user_user_name = json.result.obj;
			}
			else if (json.error) {
				console.error(json.error);
			}
		},
		get_user_session_user_name(id) {
			var _this = this;
			var user_id = { "user_id": id }
			var url = "~/api/" + _this.group_user_user_name.source_table + "/get_obj?"
			this.$get(url, user_id, function (res) {
				if (res.result && res.result.obj) {
					var arr = []
					for (let key in res.result.obj) {
						arr.push(key)
					}
					var arrForm = []
					for (let key in _this.form) {
						arrForm.push(key)
					}
					for (var i = 0; i < arr.length; i++) {
						for (var j = 0; j < arrForm.length; j++) {
							if (arr[i] === arrForm[j]) {
								if (arr[i] !== "user_name") {
									_this.form[arrForm[j]] = res.result.obj[arr[i]]
									_this.disabledObj[arrForm[j] + '_isDisabled'] = true
									break;
								} else {
									_this.disabledObj[arrForm[j] + '_isDisabled'] = true
								}
							}
						}
					}
				}
			});
		},
		get_user_user_name(id) {
			var obj = this.list_user_user_name.getObj({ "user_id": id });
			var ret = "";
			if (obj) {
				if (obj.nickname) {
					ret = obj.nickname;
				}
				else {
					ret = obj.username;
				}
			}
			return ret;
		},

		/**
		 * 获取对象之前
		 * @param {Object} param
		 */
		get_obj_before(param) {
			var form = "";
			// 获取缓存数据附加
			form = $.db.get("form");
			$.push(this.form, form);
			/**
			* 请求列表前
			* @param {Object} param
			*/
			var user_group = this.user.user_group;
			if (user_group !== "管理员") {
				switch (user_group) {
					case "用户姓名":
						if (param["user_name"] > 0) {
							param["user_name"] = this.user.user_id;
						}
						break;
				}
			}
			if (this.form && form) {
				Object.keys(this.form).forEach(key => {
					Object.keys(form).forEach(dbKey => {
						// if(dbKey === "charging_standard"){
						// 	this.form['charging_rules'] = form[dbKey];
						// 	this.disabledObj['charging_rules_isDisabled'] = true;
						// };
						if (key === dbKey) {
							this.disabledObj[key + '_isDisabled'] = true;
						}
					})
				})
			}
			$.db.del("form");
			return param;
		},

		/**
		 * 获取对象之后
		 * @param {Object} json
		 * @param {Object} func
		 */
		get_obj_after(json, func) {

		},

		is_view() {
			var bl = this.user_group == "管理员";

			if (!bl) {
				bl = this.$check_action('/individual_registration/table', 'add');
				console.log(bl ? "你有表格添加权限视作有添加权限" : "你没有表格添加权限");
			}
			if (!bl) {
				bl = this.$check_action('/individual_registration/table', 'set');
				console.log(bl ? "你有表格添加权限视作有修改权限" : "你没有表格修改权限");
			}
			if (!bl) {
				bl = this.$check_action('/individual_registration/view', 'add');
				console.log(bl ? "你有视图添加权限视作有添加权限" : "你没有视图添加权限");
			}
			if (!bl) {
				bl = this.$check_action('/individual_registration/view', 'set');
				console.log(bl ? "你有视图修改权限视作有修改权限" : "你没有视图修改权限");
			}
			if (!bl) {
				bl = this.$check_action('/individual_registration/view', 'get');
				console.log(bl ? "你有视图查询权限视作有查询权限" : "你没有视图查询权限");
			}

			console.log(bl ? "具有当前页面的查看权，请注意这不代表你有字段的查看权" : "无权查看当前页，请注意即便有字段查询权限没有页面查询权限也不行");

			return bl;
		},
		/**
		 * 上传文件
		 * @param {Object} param
		 */
		uploadimg(param) {
			this.uploadFile(param.file, "avatar");
		},

	},
	created() {
		this.get_list_user_user_name();
		this.get_group_user_user_name();
	}
}
</script>

<style>
.avatar-uploader .el-upload {
	border: 1px dashed #d9d9d9;
	border-radius: 6px;
	cursor: pointer;
	position: relative;
	overflow: hidden;
}

.avatar-uploader .el-upload:hover {
	border-color: #409EFF;
}

.avatar-uploader-icon {
	font-size: 28px;
	color: #8c939d;
	width: 178px;
	height: 178px;
	line-height: 178px;
	text-align: center;
}

.avatar {
	width: 178px;
	height: 178px;
	display: block;
}
</style>
